<?php
// 本类由系统自动生成，仅供测试用途
namespace app\dlc\controller;
use think\Db;
class UploadController extends BaseController
{

    public function index()
    {
        $this->display();
    }

    public function indeximg()
    {
        //查找带回字段
        $fbid = input('fbid');
        $isall = input('isall'); 
        $issrc = input('issrc')?1:0;
        $this -> assign('issrc',$issrc);
        $this->assign('fbid', $fbid);
        $this->assign('isall', $isall);
        $page = '1,8';
        $m = Db::name('upload');
        $cache = $m->page($page)->where(['type'=>'image'])->order('id desc')->select();
        // var_dump($cache);
        $this->assign('cache', $cache);
        return($this->fetch('indeximg'));
    }

    static function getImg($value){//通过upload  的id号。获取完整的
        $row = Db::name('upload')->where('id',$value)->find();
        $img = 'http://'.$_SERVER['HTTP_HOST'].'/public'.$row['savepath'].$row['savename'];
        $img = $row ? $img : '';
        return $img;
    }

    public function doupimg()
    {
        $files = request()->file('appfile');
        $count = 0;
        foreach ($files as $key => $file) {
            $info = $file->validate(array('size'=>20000000,'ext'=>'jpg,png,gif,jpeg'))->move(ROOT_PATH .'public'. DS . 'uploads/imgs');
            if ($info) {
                $item['name'] = $info->getInfo('name');
                $item['type'] = 'image';
                $item['savename'] = $info->getSaveName();
                $item['savepath'] = '/uploads/imgs/';
                $res  = Db::name('upload')->insert($item);
                if ($res)$count++;
                // return '/uploads/imgs/'.$info->getSaveName();
            } else {
                echo "<script>parent.doupimgcallback('" . $files->getError() . "',false)</script>";
            };
        }
        if ($count) {
            $backstr = "'" . $count . "张图片上传成功！'" . ',' . "true";
            echo "<script>parent.doupimgcallback(" . $backstr . ")</script>";
        } else {
            echo "<script>parent.doupimgcallback('图片保存时失败！',false)</script>";
        };
    }


    public function delimgs()
    {
        if (IS_AJAX) {
            $img = input(ids);
            // echo $img;
            // echo '<hr/>';
            $arr = explode('/',$img);
            $m = Db::name('upload');
            // var_dump($arr[2].'/'.$arr[3]);die;
            // $list = $m->delete($arr[2]);
            $list = $m->where(['savename'=>$arr[3].'/'.$arr[4]])->delete();
            if ($list !==false) {
                $data['status'] = 1;
                $data['msg'] = '成功删除' . $list . '张图片！';
            } else {
                $data['status'] = 0;
                $data['msg'] = '删除失败，请重试或联系管理员！';
            }
            return($data);
            // return($data, 'JSON');
        } else {
            $this->error('微专家提醒您：禁止外部访问！');
        }
    }


    public function getmoreimg()
    {
        // $_SERVER
        // $url = $_SERVER['HTTP_HOST'];
        $page = input('p') . ',8';
        $m = Db::name('upload');
        $cache = $m->page($page)->where(['type'=>'image'])->order('id desc')->select();
        // echo $page;
        // var_dump($cache);
        if ($cache) {
            $this->assign('cache', $cache);
            return($this->fetch());//封装模板fetch并返回
        } else {
            return("");
        }
    }


    //视频
    public function indexvideo()
    {
//        echo 222;
//        return(1);
        //查找带回字段
        $fbid = input('fbid');
        $isall = input('isall');
        $this->assign('fbid', $fbid);
        $this->assign('isall', $isall);
        $page = '1,8';
        $m = Db::name('upload');
        $cache = $m->page($page)->where(['type'=>'video'])->order('id desc')->select();
        $this->assign('cache', $cache);
        return ($this->fetch());
    }

    public function doupvideo()
    {
        $files = request()->file('appfile');
        $count = 0;
        foreach ($files as $key => $file) {
        $info = $file->validate(array('size'=>20000000,'ext'=>'3gp,rmvb,flv,wmv,avi,mkv,mp4,mp3,wav'))->move(ROOT_PATH .'public'. DS . 'uploads/video');
        if ($info) {
            $item['name'] = $info->getInfo('name');
            $item['type'] = 'video';
            $item['savename'] = $info->getSaveName();
            $item['savepath'] = '/uploads/video/';
            $res  = Db::name('upload')->insert($item);
            if ($res)$count++;
        } else {
            echo "<script>parent.doupimgcallback('" . $files->getError() . "',false)</script>";
        };
        }
        if ($count) {
            $backstr = "'" . $count . "个视频上传成功！'" . ',' . "true";
            echo "<script>parent.doupimgcallback(" . $backstr . ")</script>";
        } else {
            echo "<script>parent.doupimgcallback('视频保存时失败！',false)</script>";
        };
//        $config = array(
//            'mimes' => array(), //允许上传的文件MiMe类型
//            'maxSize' => 0, //上传的文件大小限制 (0-不做限制)
//            'exts' => array('3gp','rmvb','flv','wmv','avi','mkv','mp4','mp3','wav'), //允许上传的文件后缀
//            'autoSub' => true, //自动子目录保存文件
//            'subName' => array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
//            'rootPath' => './Upload/', //保存根路径
//            'savePath' => 'img/', //保存路径
//            'saveName' => array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
//            'saveExt' => '', //文件保存后缀，空则使用原后缀
//            'replace' => false, //存在同名是否覆盖
//            'hash' => true, //是否生成hash编码
//            'callback' => false, //检测文件是否存在回调，如果存在返回文件信息数组
//            'driver' => '', // 文件上传驱动
//            'driverConfig' => array(), // 上传驱动配置
//        );
//        $up = new \Think\Upload($config);
//        if ($list = $up->upload($_FILES)) {
//            $pic = Db::name('upload');
//            $count = 0;
//            $arr = array();
//            foreach ($list as $k => $v) {
//                //$arr['uid']=$uid;
//                $arr['name'] = $list[$k]['name'];
//                $arr['ext'] = $list[$k]['ext'];
//                $arr['type'] = 'video';
//                $arr['savename'] = $list[$k]['savename'];
//                $arr['savepath'] = $list[$k]['savepath'];
//                // $arr['user_id'] = $_SESSION['CMS']['uid'];
//                $re = $pic->add($arr);
//                if ($re) {
//                    $count += 1;
//                }
//            }
//
//            if ($count) {
//                $backstr = "'" . $count . "张视频上传成功！'" . ',' . "true";
//                echo "<script>parent.doupimgcallback(" . $backstr . ")</script>";
//            } else {
//                echo "<script>parent.doupimgcallback('视频保存时失败！',false)</script>";
//            };
//
//        } else {
//            echo "<script>parent.doupimgcallback('" . $up->getError() . "',false)</script>";
//        };
    }

    public function delvideos()
    {
        if (IS_AJAX) {
            $m = Db::name('upload');
            $video = $m->where(['id'=>['in',input('ids')]])->field('savepath,savename')->select();
            if($video){
                foreach ($video as $k => $v) {
                    $res[$k]=$v['savepath'].$v['savename'];
                }
            }
            $list = $m->delete(input(ids));
            if ($list == true) {
                $data['status'] = 1;
                $data['msg'] = '成功删除' . $list . '张视频！';
            } else {
                $data['status'] = 0;
                $data['msg'] = '删除失败，请重试或联系管理员！';
            }
            return($data);
        } else {
            $this->error('微专家提醒您：禁止外部访问！');
        }
    }


    public function getmorevideo()
    {

        // $_SERVER
        // $url = $_SERVER['HTTP_HOST'];
        $page = input('p') . ',8';
        $m = Db::name('upload');
        $cache = $m->page($page)->where(['type'=>'video'])->order('id desc')->select();
        // echo $page;
//         var_dump($cache);
        if ($cache) {
            $this->assign('cache', $cache);
            return($this->fetch());//封装模板fetch并返回
        } else {
            return("");
        }

    }



}